package edu.unh.sustain;

import oauth.signpost.OAuthProvider;
import oauth.signpost.basic.DefaultOAuthProvider;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
import twitter4j.Twitter;
import twitter4j.TwitterFactory;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class AuthActivity extends Activity {

    private Twitter                  twitter;
    private OAuthProvider            provider;
    private CommonsHttpOAuthConsumer consumer;

    private String                   CONSUMER_KEY    = Constants.CONSUMER_KEY;
    private String                   CONSUMER_SECRET = Constants.CONSUMER_SECRET;
    private String                   CALLBACK_URL    = "callback://UNHSustainability";

    private Button                   buttonLogin;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        System.setProperty("http.keepAlive", "false");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.twitter_oath);

        // check for saved log in
        checkForSavedLogin();

        // set consumer and provider on the Application service
        getConsumerProvider();

        // Define login button and listener
        buttonLogin = (Button) findViewById(R.id.ButtonLogin);
        buttonLogin.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                askOAuth();
            }
        });
    }

    private void checkForSavedLogin() {
        // Get Access Token and persist it
        twitter4j.auth.AccessToken a = getAccessToken();
        if (a == null)
            return;   
        
        // initialize Twitter4J
        twitter = new TwitterFactory().getInstance();
        twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
        twitter.setOAuthAccessToken(a);
        ((TwitterApplication) getApplication()).setTwitter(twitter);

        startFirstActivity();
        finish();
    }

    /**
     * start the activity to display
     */
    private void startFirstActivity() {
        Intent i = new Intent(this, TwitterActivity.class);
        startActivityForResult(i, 0);
    }

    /**
     * This method checks the shared prefs to see if we have persisted a user
     * token/secret if it has then it logs on using them, otherwise return null
     * 
     * @return AccessToken from persisted prefs
     */
    private twitter4j.auth.AccessToken getAccessToken() {
        SharedPreferences settings = getSharedPreferences(Constants.PREFS_NAME,
                MODE_PRIVATE);
        String token = settings.getString("accessTokenToken", "");
        String tokenSecret = settings.getString("accessTokenSecret", "");
        if (token != null && tokenSecret != null && !"".equals(tokenSecret)
                && !"".equals(token)) {
            return new twitter4j.auth.AccessToken(token, tokenSecret);
        }
        return null;
    }

    /**
     * Open the browser and asks the user to authorize the app. 
     * redirect the user back here.
     */
    private void askOAuth() {
        try {
            consumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY,
                    CONSUMER_SECRET);
            provider = new DefaultOAuthProvider(Constants.REQUEST_TOKEN_URL,
                    Constants.ACCESS_TOKEN_URL, Constants.AUTHORIZE_URL);
            String authUrl = provider.retrieveRequestToken(consumer,
                    CALLBACK_URL);
            Toast.makeText(this, "Please authorize this app!",
                    Toast.LENGTH_LONG).show();
            setConsumerProvider();
            Log.d("twit", "back");
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl)));
        } catch (Exception e) {
            Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
        }
    }

    /**
     * As soon as the user successfully authorized the app, we are notified
     * here. Now we need to get the verifier from the callback URL, retrieve
     * token and token_secret and feed them to twitter4j (as well as consumer
     * key and secret).
     */
    @Override
    protected void onResume() {
        super.onResume();
        if (this.getIntent() != null && this.getIntent().getData() != null) {
            Uri uri = this.getIntent().getData();
            if (uri != null && uri.toString().startsWith(CALLBACK_URL)) {
                String verifier = uri
                        .getQueryParameter(oauth.signpost.OAuth.OAUTH_VERIFIER);
                try {
                    // this will populate token and token_secret in consumer
                    provider.retrieveAccessToken(consumer, verifier);

                    // Get Access Token and persist it
                    twitter4j.auth.AccessToken a = new twitter4j.auth.AccessToken(consumer.getToken(),
                            consumer.getTokenSecret());
                    storeAccessToken(a);

                    // initialize Twitter4J
                    twitter = new TwitterFactory().getInstance();
                    twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
                    twitter.setOAuthAccessToken(a);
                    ((TwitterApplication) getApplication()).setTwitter(twitter);

                    startFirstActivity();

                } catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG)
                            .show();
                }
            }
        }
    }

    /**
     * This method persists the Access Token information so that a user is not
     * required to re-login every time the app is used
     * 
     * @param a - the access token
     */
    private void storeAccessToken(twitter4j.auth.AccessToken a) {
        SharedPreferences settings = getSharedPreferences(Constants.PREFS_NAME,
                MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString("accessTokenToken", a.getToken());
        editor.putString("accessTokenSecret", a.getTokenSecret());
        editor.commit();
    }

    /**
     * Get the consumer and provider from the application service (in the case
     * that the activity is restarted so the objects are not lost
     */
    private void getConsumerProvider() {
        OAuthProvider p = ((TwitterApplication) getApplication()).getProvider();
        if (p != null) {
            provider = p;
        }
        CommonsHttpOAuthConsumer c = ((TwitterApplication) getApplication())
                .getConsumer();
        if (c != null) {
            consumer = c;
        }
    }

    /**
     * Set the consumer and provider from the application service (in the case
     * that the activity is restarted so the objects are not lost)
     */
    private void setConsumerProvider() {
        if (provider != null) {
            ((TwitterApplication) getApplication()).setProvider(provider);
        }
        if (consumer != null) {
            ((TwitterApplication) getApplication()).setConsumer(consumer);
        }
    }

}
